﻿using CarEdge.Infrastructure.Dto.Request.Right;
using CarEdge.Infrastructure.Dto.Response.Rights;
using CarEdge.Service.Interface;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CarEdge.Admin.Controllers.Right
{
    /// <summary>
    /// 权限管理
    /// </summary>
    /// 
    [Authorize(Roles = "Admin")]
    public class RightController : BaseController
    {
        private readonly I_Rights_Service _rights_Service;
        private readonly IConfiguration _configuration;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="login_Service"></param>
        /// <param name="rights_Service"></param>
        /// <param name="configuration"></param>
        public RightController(I_Login_Service login_Service,I_Rights_Service rights_Service,IConfiguration configuration) : base(login_Service)
        {
            _rights_Service = rights_Service;
            _configuration = configuration;
        }
        /// <summary>
        /// 获取用户菜单权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_UserRight()
        {
            var user=await this.Get_Curr_User();

            var result = await _rights_Service.Get_Menus(user.Uid);
            return Ok(result);
        }
        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAllRights()
        {
            var result = await _rights_Service.GetAllRights();
            return Ok(result);
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add_Right(Right_Request_Dto dto)
        {
            var result = await _rights_Service.Add_Right(dto);
            return Ok(result);
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete_Right(Right_Request_Dto dto)
        {
            var result = await _rights_Service.Delete_Right(dto);
            return Ok(result);
        }

        /// <summary>
        /// 修改权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update_Right(Right_Request_Dto dto)
        {
            var result = await _rights_Service.Update_Right(dto);
            return Ok(result);
        }
        /// <summary>
        /// 禁/启用权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Ban_Right(Right_Request_Dto dto)
        {
            var result = await _rights_Service.Ban_Right(dto);
            return Ok(result);
        }
        /// <summary>
        /// 用户授权
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Give_Rights(Right_Request_Dto dto)
        {
            var result = await _rights_Service.Give_Rights(dto);
            return Ok(result);
        }



        /// <summary>
        /// 通过id查找权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetRightById(string id)
        {
            var result = await _rights_Service.GetRightById(id);
            return Ok(result);
        }
        /// <summary>
        /// 查询所有配置
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAllConfig(int? page = 1, int? limit = 10, string? key = "")
        {
            var result = await _rights_Service.GetAllConfig(page ?? 1, limit ?? 10, key ?? "");
            return Ok(result);
        }

        /// <summary>
        /// 查询用户所有权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetRightsByUid(string id)
        {
            var result = await _rights_Service.GetRightsByUid(id);
            return Ok(result);
        }
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAllUserRight(int? page = 1, int? limit = 10, string? key = "")
        {
            var result = await _rights_Service.GetAllUserRight(page ?? 1, limit ?? 10, key ?? "");
            return Ok(result);
        }
    }
}
